package com.junqson.je.recruit.controller;

import com.junqson.je.common.UserHolder;
import com.junqson.je.common.annotation.LoginRequire;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.common.model.User;
import com.junqson.je.recruit.model.dto.JobDTO;
import com.junqson.je.recruit.model.vo.JobVo;
import com.junqson.je.recruit.service.JobService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/5 14:13
 * @Version 1.0
 */
@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    private JobService jobService;


    /**
     * 发布职位
     *
     * @return
     */
    @LoginRequire(needAuth = true)
    @RequestMapping("publish")
    public ApiResult publishJob(@RequestBody JobDTO jobDTO) {

        if (jobDTO == null) {
            return ApiResult.error("job dto is null");
        }
        if (StringUtils.isEmpty(jobDTO.getTitle()) || StringUtils.isEmpty(jobDTO.getLocation()) ||
                StringUtils.isEmpty(jobDTO.getCategory())) {

            return ApiResult.error("标题/地点/类目不能为空");
        }

        jobDTO.setJobId(null);
        jobService.publishJob(jobDTO);
        return ApiResult.success();
    }

    /**
     * 关闭职位
     *
     * @return
     */
    @LoginRequire(needAuth = true)
    @RequestMapping("close")
    public ApiResult removeJob(Integer jobId) {
        if (jobId == null) {
            return ApiResult.error("job id is null");
        }
        return jobService.closeJob(jobId);
    }

    /**
     * 通过id获取职位详情
     *
     * @param jobId
     * @return
     */
    @RequestMapping("getById")
    public ApiResult getByJobId(Integer jobId) {
        if (jobId == null) {
            return ApiResult.error("job id is null");
        }
        return ApiResult.success(jobService.getByJobId(jobId));
    }


    /**
     * 修改职位信息
     */
    @RequestMapping("update")
    @LoginRequire(needAuth = true)
    public ApiResult updateJobInfo(@RequestBody JobDTO jobDTO) {
        if (jobDTO == null) {
            return ApiResult.error("job dto is null");
        }
        if (jobDTO.getJobId() == null) {
            return ApiResult.error("job id is null");
        }
        return jobService.updateJobInfo(jobDTO);
    }


    /**
     * 查看个人已发布的职位
     */
    @LoginRequire(needAuth = true)
    @RequestMapping("published")
    public ApiResult publishedJobs() {
        User user = UserHolder.get();
        int userId = user.getUserId();
        return ApiResult.success(jobService.getJobByPublisherId(userId));
    }

    /**
     * 查看某个认证用户发布的职位 不显示关闭的职位
     *
     * @param userId
     * @return
     */
    @RequestMapping("publishedByUser")
    public ApiResult publishedJobsByUser(Integer userId) {
        if (userId == null) {
            return ApiResult.error("user id is null");
        }
        return ApiResult.success(jobService.getOpenJobByPublisherId(userId));
    }

    /**
     * 分页查看职位, 按照时间排序，根据地点和类目过滤
     */
    @RequestMapping("/pageAll")
    public ApiResult pageJobs(String location, String category, Integer page, Integer size) {
        size = 20;
        page = (int) (Math.random() * 20);
        List<JobVo> vos = Collections.emptyList();
        if (StringUtils.isEmpty(location) && StringUtils.isEmpty(category)) {
            vos = jobService.pageAllJob(page, size);
        } else if (StringUtils.isNotEmpty(location) && StringUtils.isNotEmpty(category)) {
            vos = jobService.pageJobByLocationAndCategory(location, category, page, size);
        } else {
            if (StringUtils.isNotEmpty(location)) {
                vos = jobService.pageJobBylocation(location, page, size);
            }
            if (StringUtils.isNotEmpty(category)) {
                vos = jobService.pageJobByCategory(category, page, size);
            }
        }
        return ApiResult.success(vos);
    }
    @RequestMapping("/pageAllprs")
    public ApiResult pageJobsPrs(String location, String category, Integer page, Integer size) {
//        size = 20;
//        page = (int) (Math.random() * 20);
        List<JobVo> vos = Collections.emptyList();
        if (StringUtils.isEmpty(location) && StringUtils.isEmpty(category)) {
            vos = jobService.pageAllJob(page, size);
        } else if (StringUtils.isNotEmpty(location) && StringUtils.isNotEmpty(category)) {
            vos = jobService.pageJobByLocationAndCategory(location, category, page, size);
        } else {
            if (StringUtils.isNotEmpty(location)) {
                vos = jobService.pageJobBylocation(location, page, size);
            }
            if (StringUtils.isNotEmpty(category)) {
                vos = jobService.pageJobByCategory(category, page, size);
            }
        }
        return ApiResult.success(vos);
    }



//    @RequestMapping("pageByLocation")
//    public ApiResult pageByLocation(String location, Integer page, Integer size) {
//        if (StringUtils.isEmpty(location)) {
//            return ApiResult.error("location is empty");
//        }
//        return ApiResult.success(jobService.pageJobBylocation(location, page, size));
//    }
//
//
//    @RequestMapping("pageByCategory")
//    public ApiResult pageByCategory(String category, Integer page, Integer size) {
//        if (StringUtils.isEmpty(category)) {
//            return ApiResult.error("category is empty");
//        }
//        return ApiResult.success(jobService.pageJobByCategory(category, page, size));
//    }

}
